package com.da.springtest.condition.test3;

import java.util.Properties;

import com.da.springtest.util.PropertiesUtil;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class EnvCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        String envStr = null;
        Properties properties = PropertiesUtil.readPropertiesFile();
        if (properties != null) {
            envStr = properties.getProperty("env");
        }
        //获取使用条件的类上的EnvCondition注解中对应的环境
        EnvConditional.Env env = (EnvConditional.Env)metadata.getAllAnnotationAttributes(EnvConditional.class.getName())
            .get("value").get(0);
        return env.name().equalsIgnoreCase(envStr);
    }
}